package com.netease.financial.base.push;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;

import com.netease.financial.NfsApplication;
import com.netease.financial.common.util.NfsLog;
import com.netease.financial.module.main.MainActivity;

import org.json.JSONObject;

/**
 * Created by wyf on 8/7/15.
 */
public class PushEventHelper {

    private static final String TAG = PushEventHelper.class.getSimpleName();

    private static final class NestedInstanceClass {
        private static final PushEventHelper INSTANCE = new PushEventHelper();
    }

    private PushEventManager mPushEventManager;

    private PushEventHelper() {
        mPushEventManager = new PushEventManager();
        registerPushEvents();
    }

    private static PushEventHelper getInstance() {
        return NestedInstanceClass.INSTANCE;
    }

    public static void executeNotificationPushEvent(Context context, Bundle bundle) {
        PushEventHelper.getInstance().executeNotificationPushEventInternal(context, bundle);
    }

    private void executeNotificationPushEventInternal(Context context, Bundle bundle) {
        String message = PushUtils.getPushMessage(bundle);
//        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
//        String jsonStr = bundle.getString(JPushInterface.EXTRA_EXTRA);
        JSONObject jsonObject = PushUtils.getExtraJSONObject(bundle);

        String pushType = PushUtils.getPushType(jsonObject);
        PushEventManager.Function function = mPushEventManager.getFunctionForNotificationPushEvent(pushType);
        if (function != null) {
            function.onExecute(context, message, jsonObject);
        }
//        sendIncomingPushEventBroadcast(context, pushType, bundle);
    }

    public static void executeCustomPushEvent(Context context, Bundle bundle) {
        PushEventHelper.getInstance().executeCustomPushEventInternal(context, bundle);
    }

    private void executeCustomPushEventInternal(Context context, Bundle bundle) {
        String message = PushUtils.getPushMessage(bundle);
//        String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
//        String jsonStr = bundle.getString(JPushInterface.EXTRA_EXTRA);
        JSONObject jsonObject = PushUtils.getExtraJSONObject(bundle);

        String pushType = PushUtils.getPushType(jsonObject);
        PushEventManager.Function function = mPushEventManager.getFunctionForCustomPushEvent(pushType);
        if (function != null) {
            function.onExecute(context, message, jsonObject);
        }
        sendIncomingPushEventBroadcast(context, bundle);
    }

    public static void sendIncomingPushEventBroadcast(Context context, Bundle bundle) {
//        String jsonStr = bundle.getString(JPushInterface.EXTRA_EXTRA);
        JSONObject jsonObject = PushUtils.getExtraJSONObject(bundle);
        String pushType = PushUtils.getPushType(jsonObject);
        NfsLog.d(TAG, "sendIncomingPushEventBroadcast.pushType:" + pushType);
        Intent intent = new Intent(PushConstants.getPushAction(pushType));
//        intent.putExtra(pushType, bundle);
        intent.putExtras(bundle);
        context.sendBroadcast(intent);
    }

    private void registerNotificationPushEvent(String pushType, PushEventManager.Function function) {
        mPushEventManager.registerNotificationPushEvent(pushType, function);
    }

    private void registerCustomPushEvent(String pushType, PushEventManager.Function function) {
        mPushEventManager.registerCustomPushEvent(pushType, function);
    }

    private void registerPushEvents() {
        registerNotificationPushEvent(PushConstants.PUSH_TYPE_SITE_MESSAGE, new PushEventManager.Function() {
            @Override
            public void onExecute(Context context, String message, JSONObject jsonObject) {
                onUserClickSiteMessageNotification(context, message);
            }
        });
        registerCustomPushEvent(PushConstants.PUSH_TYPE_SITE_MESSAGE, new PushEventManager.Function() {
            @Override
            public void onExecute(Context context, String message, JSONObject jsonObject) {
                showNotification(context, message);
            }
        });
    }

    private void onUserClickSiteMessageNotification(Context context, String contentText) {
        NfsApplication.getApplication().getNavigator().navigateToNotificationDetailScreen(context);
    }

    private void showNotification(Context context, String contentText) {
        Intent resultIntent = MainActivity.getCallingIntent(context, MainActivity.TAB_POSITION_ASSET);
        resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);

        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        ApplicationInfo appInfo = context.getApplicationInfo();
        Notification notification = new NotificationCompat.Builder(context)
                .setTicker(contentText)
                .setContentTitle(context.getResources().getString(appInfo.labelRes))
                .setContentText(contentText).setSmallIcon(appInfo.icon)
                .setContentIntent(pendingIntent).build();

        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;  // 设置为铃声与震动都要
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        nm.notify(0, notification);
    }
}
